package com.lesson32.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lesson32.model.PayDetailVo;
import com.lesson32.model.ProductItemVo;
import com.lesson32.model.UserLoginVo;

@RestController
public class ShoppingController {

	@RequestMapping(value={"/user/login"})
	public Map<String, Object> userLoing(@RequestBody UserLoginVo userLoginVo){
		Map<String, Object> results = new HashMap<String, Object>();
		System.out.println(userLoginVo.getUserName() +", "+ userLoginVo.getPassword());
		int random = new Random().nextInt(99);
		long sleep = random * 10;
		try {
			Thread.sleep(sleep);
		} catch (InterruptedException e) {
		}
		 results.put("code", "000000");
		 results.put("msg", "success");
		 Map<String, Object> body = new HashMap<String, Object>();
		 body.put("token", UUID.randomUUID().toString());
		 results.put("body", body);
		 return results;
	}
	
	@RequestMapping(value={"/user/createOrder"})
	public Map<String, Object> createOrder(@RequestBody ProductItemVo productItemVo){
		Map<String, Object> results = new HashMap<String, Object>();
		if(StringUtils.isEmpty(productItemVo.getToken()) || productItemVo.getToken().contains("$")){
			 results.put("code", "000001");
			 results.put("msg", "not login");
			 return results;
		}
		if(StringUtils.isEmpty(productItemVo.getProductId())|| productItemVo.getProductId().contains("$")){
			 results.put("code", "000002");
			 results.put("msg", "not product item");
			 return results;
		}
		int price = new Random().nextInt(99);
		results.put("code", "000000");
		results.put("msg", "success");
		results.put("body", price);
		return results;
	}
	
	@RequestMapping(value={"/user/pay"})
	public Map<String, Object> pay(@RequestBody PayDetailVo payDetail){
		Map<String, Object> results = new HashMap<String, Object>();
		if(StringUtils.isEmpty(payDetail.getToken())||payDetail.getToken().contains("$")){
			 results.put("code", "000001");
			 results.put("msg", "not login");
			 return results;
		}
		if(payDetail.getPrice()<=0){
			results.put("code", "000001");
			 results.put("msg", "price zero");
			 return results;
		}
		results.put("code", "000000");
		results.put("msg", "success");
		return results;
	}
}
